﻿//==============================================================================
// File: ShellMenuController.cs
// Created: 2010-05-10
// Author: Piotr Włodek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using WpfRcp.Core.Globals;
using WpfRcp.Core.Services;
using WpfRcp.Core.Services.Commands;
using WpfRcp.Core.Views.Presenters;
using System.Windows.Media.Imaging;
using System;

namespace WpfRcp.Controllers
{
    public class ShellMenuController
    {
        private readonly IActionRegistry m_ActionRegistry;
        private readonly IMenuRegistry m_menuRegistry;

        private readonly IApplicationSettings m_Settings;

        public ShellMenuController(IActionRegistry actionRegistry, IMenuRegistry menuRegistry, IApplicationSettings settings)
        {
            m_ActionRegistry = actionRegistry;
            m_menuRegistry = menuRegistry;

            m_Settings = settings;
        }

        public void SetupMenu(IShellPresentationModel presentationModel)
        {
            m_ActionRegistry.RegisterCommandCategory(ApplicationConstants.CommandCategories.MammothCommands);

            var command = m_ActionRegistry.RegisterCommand(ApplicationConstants.Commands.Exit,
                                                           ApplicationConstants.CommandCategories.MammothCommands,
                                                           presentationModel.ExitCommand);
            command.KeyGestures.Add(new CommandKeyGesture(Key.X, ModifierKeys.Control));

            command = m_ActionRegistry.RegisterCommand(ApplicationConstants.Commands.ChangeHeaderVisibility,
                                                       ApplicationConstants.CommandCategories.MammothCommands,
                                                       presentationModel.ChangeHeaderVisibilityCommand);
            command.KeyGestures.Add(new CommandKeyGesture(Key.F10));

            command = m_ActionRegistry.RegisterCommand(ApplicationConstants.Commands.ToggleFullScreen,
                                                       ApplicationConstants.CommandCategories.MammothCommands,
                                                       presentationModel.ToggleFulscreenCommand);
            command.KeyGestures.Add(new CommandKeyGesture(Key.F11));

            m_ActionRegistry.RegisterCommand(ApplicationConstants.Commands.OpenAboutView,
                                             ApplicationConstants.CommandCategories.MammothCommands,
                                             presentationModel.OpenAboutViewCommand);

            m_ActionRegistry.RegisterCommand(ApplicationConstants.Commands.OpenLogView,
                                             ApplicationConstants.CommandCategories.MammothCommands,
                                             presentationModel.OpenLogViewCommand);

            m_ActionRegistry.RegisterCommand(ApplicationConstants.Commands.OpenStatusView,
                                             ApplicationConstants.CommandCategories.MammothCommands,
                                             presentationModel.OpenStatusViewCommand);

            m_ActionRegistry.RegisterCommand(ApplicationConstants.Commands.OpenOptionsView,
                                             ApplicationConstants.CommandCategories.MammothCommands,
                                             presentationModel.OpenApplicationOptionsCommand);

            // File menu
            var menuTab = m_menuRegistry.RegisterMenuTab(ApplicationConstants.Menus.Level1.File);
            menuTab.Position = 0;
            menuTab.Text = Properties.Resources.FileMenuItem;

            // View menu
            menuTab = m_menuRegistry.RegisterMenuTab(ApplicationConstants.Menus.Level1.View);
            menuTab.Position = 20;
            menuTab.Text = Properties.Resources.ViewMenuItem;

            // Help menu
            menuTab = m_menuRegistry.RegisterMenuTab(ApplicationConstants.Menus.Level1.Help);
            menuTab.Position = 100;
            menuTab.Text = Properties.Resources.HelpMenuItem;

            var group = m_menuRegistry.RegisterMenuItemGroup(ApplicationConstants.MenuGroups.Exit, 
                                                            ApplicationConstants.Menus.Level1.File);
            group.Position = 100;
            group.Text = Properties.Resources.MenuGroupExit;

            // Exit menu
            var menuItem = m_menuRegistry.RegisterMenuItem(ApplicationConstants.Menus.Level2.Exit,
                                                         ApplicationConstants.MenuGroups.Exit,
                                                         ApplicationConstants.Commands.Exit);
            menuItem.Position = 0;
            menuItem.Text = Properties.Resources.ExitMenuItem;

            group = m_menuRegistry.RegisterMenuItemGroup(ApplicationConstants.MenuGroups.BasicView, 
                                                        ApplicationConstants.Menus.Level1.View);
            group.Position = 0;
            group.Text = Properties.Resources.MenuGroupBasicView;

            // HeaderVisibility menu
            menuItem = m_menuRegistry.RegisterMenuItem(ApplicationConstants.Menus.Level2.HeaderVisibility,
                                                         ApplicationConstants.MenuGroups.BasicView,
                                                         ApplicationConstants.Commands.ChangeHeaderVisibility);
            menuItem.Position = 0;
            menuItem.Text = Properties.Resources.HeaderVisibilityMenuItem;
            menuItem.VisualCreated += ((sender, e) =>
            {
                var item = m_menuRegistry.MenuItems[ApplicationConstants.Menus.Level2.HeaderVisibility];
                var binding = new Binding("IsHeaderVisible") { Source = m_Settings };
                BindingOperations.SetBinding(item.Visual, MenuItem.IsCheckedProperty, binding);
            });

            // FullScreen menu
            menuItem = m_menuRegistry.RegisterMenuItem(ApplicationConstants.Menus.Level2.FullScreen,
                                                         ApplicationConstants.MenuGroups.BasicView,
                                                         ApplicationConstants.Commands.ToggleFullScreen);
            menuItem.Position = 1;
            menuItem.Text = Properties.Resources.FullscreenMenuItem;
            menuItem.VisualCreated += ((sender, e) =>
            {
                var item = m_menuRegistry.MenuItems[ApplicationConstants.Menus.Level2.FullScreen];
                var binding = new Binding("IsFullScreen") { Source = m_Settings };
                BindingOperations.SetBinding(item.Visual, MenuItem.IsCheckedProperty, binding);
            });

            group = m_menuRegistry.RegisterMenuItemGroup(ApplicationConstants.MenuGroups.ViewWindows, 
                                                        ApplicationConstants.Menus.Level1.View);
            group.Position = 10;
            group.Text = Properties.Resources.MenuGroupViewWindows;

            // OptionsView menu
            menuItem = m_menuRegistry.RegisterMenuItem(ApplicationConstants.Menus.Level2.OptionsView,
                                                         ApplicationConstants.MenuGroups.ViewWindows,
                                                         ApplicationConstants.Commands.OpenOptionsView);
            menuItem.Position = 0;
            menuItem.Text = Properties.Resources.OptionsMenuItem;

            // LogView menu
            menuItem = m_menuRegistry.RegisterMenuItem(ApplicationConstants.Menus.Level2.LogView,
                                                         ApplicationConstants.MenuGroups.ViewWindows,
                                                         ApplicationConstants.Commands.OpenLogView);
            menuItem.Position = 1;
            menuItem.Text = Properties.Resources.LogWindowMenuItem;

            // StatusView menu
            menuItem = m_menuRegistry.RegisterMenuItem(ApplicationConstants.Menus.Level2.StatusView,
                                                         ApplicationConstants.MenuGroups.ViewWindows,
                                                         ApplicationConstants.Commands.OpenStatusView);
            menuItem.Position = 2;
            menuItem.Text = Properties.Resources.StatusWindowMenuItem;

            group = m_menuRegistry.RegisterMenuItemGroup(ApplicationConstants.MenuGroups.About, 
                                                        ApplicationConstants.Menus.Level1.Help);
            group.Position = 100;
            group.Text = Properties.Resources.MenuGroupAbout;

            var icon = new System.Windows.Controls.Image() 
            { 
                Source = new System.Windows.Media.Imaging.BitmapImage(
                    new Uri(@"pack://application:,,,/WpfRcp;component/Resources/Images/infosmall.png", UriKind.Absolute))
            };
            menuItem = m_menuRegistry.RegisterMenuItem(ApplicationConstants.Menus.Level2.AboutView,
                                                         ApplicationConstants.MenuGroups.About,
                                                         ApplicationConstants.Commands.OpenAboutView, icon);
            menuItem.Position = 0;
            menuItem.Text = Properties.Resources.AboutMenuItem;
        }
    }
}